﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using XChip_Common;

namespace XChipTool
{
    public partial class XChip_VersionInfo : DevExpress.XtraEditors.XtraForm
    {
        public XChip_VersionInfo()
        {
            InitializeComponent();
             
        }
       
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(FW_year.Text)||string.IsNullOrEmpty(FW_month.Text)||string.IsNullOrEmpty(FW_day.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"FW 文件框不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(HW_year.Text) || string.IsNullOrEmpty(HW_month.Text) || string.IsNullOrEmpty(HW_day.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show($"HW 文件框不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (checkEdit23.Checked)
            {
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13a7").PValue = $"0x01";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b8").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b9").PValue = $"0x01";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ba").PValue = $"0x04";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bb").PValue = $"0x05";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bc").PValue = $"0x02";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bd").PValue = $"0x03";
            }
            else
            {
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13a7").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b8").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b9").PValue = $"0x01";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ba").PValue = $"0x02";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bb").PValue = $"0x03";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bc").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bd").PValue = $"0x00";
            }

            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13be").PValue = (FW_year.Text).PadLeft(4, '0').Substring(0, 1) == "0" ? "0x25" : $"0x0{(FW_year.Text).PadLeft(4, '0').Substring(0, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bf").PValue = (FW_year.Text).PadLeft(4, '0').Substring(1, 1) == "0" ? "0x25" : $"0x0{(FW_year.Text).PadLeft(4, '0').Substring(1, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c0").PValue = (FW_year.Text).PadLeft(4, '0').Substring(2, 1) == "0" ? "0x25" : $"0x0{(FW_year.Text).PadLeft(4, '0').Substring(2, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c1").PValue = (FW_year.Text).PadLeft(4, '0').Substring(3, 1) == "0" ? "0x25" : $"0x0{(FW_year.Text).PadLeft(4, '0').Substring(3, 1)}";

            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c3").PValue = (FW_month.Text).PadLeft(2, '0').Substring(0, 1) == "0" ? "0x25" : $"0x0{(FW_month.Text).PadLeft(2, '0').Substring(0, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c4").PValue = (FW_month.Text).PadLeft(2, '0').Substring(1, 1) == "0" ? "0x25" : $"0x0{(FW_month.Text).PadLeft(2, '0').Substring(1, 1)}";

            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c6").PValue = (FW_day.Text).PadLeft(2, '0').Substring(0, 1) == "0" ? "0x25" : $"0x0{(FW_day.Text).PadLeft(2, '0').Substring(0, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c7").PValue = (FW_day.Text).PadLeft(2, '0').Substring(1, 1) == "0" ? "0x25" : $"0x0{(FW_day.Text).PadLeft(2, '0').Substring(1, 1)}";

            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ca").PValue = (HW_year.Text).PadLeft(4, '0').Substring(0, 1) == "0" ? "0x25" : $"0x0{(HW_year.Text).PadLeft(4, '0').Substring(0, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cb").PValue = (HW_year.Text).PadLeft(4, '0').Substring(1, 1) == "0" ? "0x25" : $"0x0{(HW_year.Text).PadLeft(4, '0').Substring(1, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cc").PValue = (HW_year.Text).PadLeft(4, '0').Substring(2, 1) == "0" ? "0x25" : $"0x0{(HW_year.Text).PadLeft(4, '0').Substring(2, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cd").PValue = (HW_year.Text).PadLeft(4, '0').Substring(3, 1) == "0" ? "0x25" : $"0x0{(HW_year.Text).PadLeft(4, '0').Substring(3, 1)}";

            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cf").PValue = (HW_month.Text).PadLeft(2, '0').Substring(0, 1) == "0" ? "0x25" : $"0x0{(HW_month.Text).PadLeft(2, '0').Substring(0, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13d0").PValue = (HW_month.Text).PadLeft(2, '0').Substring(1, 1) == "0" ? "0x25" : $"0x0{(HW_month.Text).PadLeft(2, '0').Substring(1, 1)}";

            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13d2").PValue = (HW_day.Text).PadLeft(2, '0').Substring(0, 1) == "0" ? "0x25" : $"0x0{(HW_day.Text).PadLeft(2, '0').Substring(0, 1)}";
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13d3").PValue = (HW_day.Text).PadLeft(2, '0').Substring(1, 1) == "0" ? "0x25" : $"0x0{(HW_day.Text).PadLeft(2, '0').Substring(1, 1)}";

            if (radioGroup1.SelectedIndex == 0)
            {
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13aa").PValue = $"0x01";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ac").PValue = $"0x{Convert.ToInt32(frame_limit.Text).ToString("X4").Substring(2, 2)}";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ad").PValue = $"0x{Convert.ToInt32(frame_limit.Text).ToString("X4").Substring(0, 2)}";
                
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x125c").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b0").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b1").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b4").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b5").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b6").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b7").PValue = $"0x00";
            }
            else
            {
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13aa").PValue = $"0x02";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ac").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ad").PValue = $"0x00";

                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x125c").PValue = $"0x01";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b0").PValue = $"0x{Convert.ToInt32(frame_limit.Text).ToString("X4").Substring(2, 2)}";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b1").PValue = $"0x{Convert.ToInt32(frame_limit.Text).ToString("X4").Substring(0, 2)}";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b4").PValue = $"0xE8";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b5").PValue = $"0x03";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b6").PValue = $"0x00";
                ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b7").PValue = $"0x00";
            }
            ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13a6").PValue = $"0x01";
            this.Close();
        }

        private void XChip_VersionInfo_Load(object sender, EventArgs e)
        {
            FW_year.Text = $"{ ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13be").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13bf").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c0").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c1").PValue}".Replace("0x0", "").Replace("0x25", "0");
            if (FW_year.Text == "0000")
            {
                FW_year.Text = DateTime.Now.Year.ToString();
            }
            FW_month.Text = $"{ ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c3").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c4").PValue}".Replace("0x0", "").Replace("0x25", "0");
            if (FW_month.Text =="00")
            {
                FW_month.Text = DateTime.Now.Month.ToString("00");
            }
            FW_day.Text = $"{ ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c6").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13c7").PValue}".Replace("0x0", "").Replace("0x25", "0");
            if (FW_day.Text == "00")
            {
                FW_day.Text = DateTime.Now.Day.ToString("00");
            }

            HW_year.Text = $"{ ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ca").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cb").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cc").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cd").PValue}".Replace("0x0", "").Replace("0x25", "0"); 
            if (HW_year.Text=="0000")
            {
                HW_year.Text = DateTime.Now.Year.ToString();
            }
            HW_month.Text = $"{ ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13cf").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13d0").PValue}".Replace("0x0", "").Replace("0x25", "0");
            if (HW_month.Text == "00")
            {
                HW_month.Text = DateTime.Now.Month.ToString("00");
            }
            HW_day.Text = $"{ ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13d2").PValue}".Replace("0x0", "").Replace("0x25", "0") +
                        $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13d3").PValue}".Replace("0x0", "").Replace("0x25", "0");
            if (HW_day.Text == "00")
            {
                HW_day.Text = DateTime.Now.Day.ToString("00");
            }
            if (ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13aa").PValue == "0x01")
            {
                radioGroup1.SelectedIndex = 0;
                frame_limit.Text = Convert.ToInt32( $"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ad").PValue}{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13ac").PValue}".Replace("0x", ""),16).ToString();
            }
            else
            {
                radioGroup1.SelectedIndex = 1;
                frame_limit.Text = Convert.ToInt32($"{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b1").PValue}{ParaHelp.Osd_Version_Info.Find(p => p.PKey == "0x13b0").PValue}".Replace("0x", ""), 16).ToString();
            }

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            ParaHelp.CleanOsdVersionInfo();
            this.Close();
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 0)
            {
                frame_limit.Text = "300";
            }
            else
            {
                frame_limit.Text = "100";
            }
           
        }
    }
}